package com.youlg.snake;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Random;

public class GamePanel extends JPanel implements KeyListener,ActionListener{

    int lenth;//蛇的长度
    int[] snakeX = new int[600];
    int[] snakeY = new int[500];
    String fx = "T";//R:右 ，L：左 ，T：上 ， D：下

    boolean isStart = false;//游戏是否开始

    Timer timer = new Timer(100,this);//定时器

    //定义一个食物
    int foodx;
    int foody;
    Random random = new Random();

    //死亡判定
    boolean isFail = false;

    //积分系统
    int score;

//    构造器
    public GamePanel(){
        init();
        //获取键盘的监听事件
        this.setFocusable(true);
        this.addKeyListener(this);
        timer.start();//让时间动起来
    }

    //    初始化
    public void init(){
        lenth = 3;
        snakeX[0] = 100; snakeY[0] = 100;//头部坐标
        snakeX[1] = 75; snakeY[1] = 100;//第一个身体坐标
        snakeX[2] = 50; snakeY[2] = 100;//第二个身体坐标
        fx = "R";

        foodx = 25 + 25 * random.nextInt(34);
        foody = 75 + 25 * random.nextInt(24);

        score = 0;
    }

    //画板：画界面、画蛇
    //Graphics:画笔
    @Override
    protected void paintComponent(Graphics g) {

        super.paintComponent(g);//清屏
        this.setBackground(Color.BLACK);//设置背景的颜色

        Data.header.paintIcon(this,g,25,11);//绘制头部的广告栏
        g.fillRect(25,75,850,600);//绘制游戏区域



        //画一个静态的小蛇
        if( "R".equals(fx)){
            Data.right.paintIcon(this,g,snakeX[0],snakeY[0]);
        }else if("L".equals(fx)){
            Data.left.paintIcon(this,g,snakeX[0],snakeY[0]);
        }else if("T".equals(fx)){
            Data.top.paintIcon(this,g,snakeX[0],snakeY[0]);
        }else if("D".equals(fx)){
            Data.down.paintIcon(this,g,snakeX[0],snakeY[0]);
        }


        for (int i = 1; i < lenth; i++) {//蛇的身体长度由lenth来控制
            Data.body.paintIcon(this,g,snakeX[i],snakeY[i]);
        }

        //画积分
        g.setColor(Color.BLACK);
        g.setFont(new Font("微软雅黑",Font.BOLD,18));
        g.drawString("长度："+lenth,750,35);
        g.drawString("分数："+score,750,60);

        //画食物
        Data.food.paintIcon(this,g,foodx,foody);

        //游戏提示：是否开始
        if(isStart==false){
            //画一个文字，String
            g.setColor(Color.WHITE);//设置画笔的颜色
            g.setFont(new Font("微软雅黑",Font.BOLD,40));//设置字体
            g.drawString("按下空格开始游戏",300,300);
        }

        //失败提醒
        if(isFail){
            g.setColor(Color.RED);//设置画笔的颜色
            g.setFont(new Font("微软雅黑",Font.BOLD,40));//设置字体
            g.drawString("游戏失败，按下空格重新开始游戏",150,300);
        }

    }

    //键盘按下，未释放
    @Override
    public void keyPressed(KeyEvent e) {
        //获取按下的键盘是哪个键
        int keyCode = e.getKeyCode();

        if(keyCode == KeyEvent.VK_SPACE){//如果按下的是空格
            if(isFail){//失败重来
                isFail = false;
                init();//重新初始化游戏
            }else{//暂停游戏
                isStart = !isStart;
            }
            repaint();//刷新界面
        }

        //键盘控制反向
        if(keyCode == KeyEvent.VK_LEFT){
            fx = "L";
        }else if(keyCode == KeyEvent.VK_RIGHT){
            fx = "R";
        }else if(keyCode == KeyEvent.VK_UP){
            fx = "T";
        }else if(keyCode == KeyEvent.VK_DOWN){
            fx = "D";
        }

    }

    @Override
    public void keyTyped(KeyEvent e) {
        //键盘按下
    }

    @Override
    public void keyReleased(KeyEvent e) {
        //释放某个键盘
    }


    //定时器，监听时间，帧：执行定时操作
    @Override
    public void actionPerformed(ActionEvent e) {
        //如果游戏处于开始状态，并且游戏没有结束
        if(isStart && !isFail){
            //右移
            for(int i = lenth-1 ; i > 0 ; i--){
                snakeX[i] = snakeX[i-1];
                snakeY[i] = snakeY[i-1];
            }

            //通过控制方向让头部移动
            if("R".equals(fx)){
                snakeX[0] = snakeX[0] + 25;//头部移动
                if(snakeX[0] > 850){snakeX[0] = 25;}//边界判断
            }else if("L".equals(fx)){
                snakeX[0] = snakeX[0] - 25;//头部移动
                if(snakeX[0] < 25){snakeX[0] = 850;}//边界判断
            }else if("T".equals(fx)){
                snakeY[0] = snakeY[0] - 25;//头部移动
                if(snakeY[0] < 75){snakeY[0] = 650;}//边界判断
            }else if("D".equals(fx)){
                snakeY[0] = snakeY[0] + 25;//头部移动
                if(snakeY[0] > 650){snakeY[0] = 75;}//边界判断
            }

            if(snakeX[0] == foodx && snakeY[0] == foody){
                //长度+1
                lenth++;
                score = score + 10;

                snakeX[lenth-1] = foodx-1;
                snakeY[lenth-1] = foody-1;
                //重新生成食物
                foodx = 25 + 25 * random.nextInt(34);
                foody = 75 + 25 * random.nextInt(24);
            }

            //结束判断
            for (int i = 1; i < lenth; i++) {
                if(snakeX[0]==snakeX[i] && snakeY[0] == snakeY[i]){
                    isFail = true;
                }
            }

            //刷新界面
            repaint();
        }
        timer.start();//让时间动起来
    }



}
